import { EventTypeConst } from "../Const/EventTypeConst";



export interface IEventData {
    data?: any;
    name: EventTypeConst;
}
class listenerModel {
    public msgType: string;
    public tag: string;
    public listener: any;
    public receiveHandle: (event: IEventData) => void;
    public isOnce: boolean = false;
    constructor(eventType: string, listener: any, completeCallback: (event: IEventData) => void, _tag?: string) {
        this.msgType = eventType;
        this.tag = _tag;
        this.listener = listener;
        this.receiveHandle = completeCallback;
    }
}
export class EventComponent {

    private eventList = {};
    public addEventListener(msgType: EventTypeConst, completeCallback: (event: IEventData) => void, listener: any, tag?: string): void {
        const eventType: string = msgType as string;
        const newListener: listenerModel = new listenerModel(eventType, listener, completeCallback, tag);
        if (!this.eventList[eventType]) {
            this.eventList[eventType] = [];
        }
        // 查询是否有这个监听者 防止重复添加监听
        for (const key in this.eventList[eventType]) {
            const curListener: listenerModel = this.eventList[eventType][key];
            if (curListener.listener == listener) {
                //AppLog.warn("can not repeat add same listener:" + msgType);
                return;
            }
        }
        this.eventList[eventType].push(newListener);
    }

    // 单次监听 收到消息后即刻销毁
    public addOnceEventListener(msgType: EventTypeConst, completeCallback: (event: IEventData) => void, listener: any): void {
        const eventType: string = msgType as string;
        const newListener: listenerModel = new listenerModel(eventType, listener, completeCallback, undefined);
        newListener.isOnce = true;
        if (!this.eventList[eventType]) {
            this.eventList[eventType] = [];
        }
        // 查询是否有这个监听者 防止重复添加监听
        for (const key in this.eventList[eventType]) {
            const curListener: listenerModel = this.eventList[eventType][key];
            if (curListener.listener == listener) {
                //AppLog.warn("can not repeat add same listener:" + msgType);
                return;
            }
        }
        this.eventList[eventType].push(newListener);
    }
    public sendEvent(_msgType: EventTypeConst, _data?: any): void {
        if (!this.eventList[_msgType]) {
            // cc.log("msg =" + _msgType + " listener is null!" );
            return;
        }
        if (this.eventList[_msgType].length > 0) {
            for (let i: number = 0; i < this.eventList[_msgType].length; i++) {
                const listener: listenerModel = this.eventList[_msgType][i];
                const sendData: IEventData = { name: _msgType, data: _data };
                // listener.receiveHandle(sendData,listener.listener);
                listener.receiveHandle.call(listener.listener, sendData);
                if (listener.isOnce) {
                    this.removeEventByListener(listener);
                }
            }
        }
    }

    public removeAllEventListeners(): void {
        this.eventList = {};
    }

    public removeEventByListener(listener: any) {

        for (const msyType in this.eventList) {
            for (let i: number = 0; i < this.eventList[msyType].length; i++) {
                if (this.eventList[msyType][i].listener == listener) {
                    // cc.log("remove Event success! event =  ",this.eventList[msyType][i].msgType);
                    this.eventList[msyType].splice(i, 1);

                }
            }
        }

    }

    public removeEventListenersByTag(tag: string): void {

        for (const msyType in this.eventList) {
            for (let i: number = 0; i < this.eventList[msyType].length; i++) {
                if (this.eventList[msyType][i].tag == tag) {
                    cc.log("remove Event success! event =  ", this.eventList[msyType][i].msgType);
                    this.eventList[msyType].splice(i, 1);

                }
            }
        }

    }

}
